package presenter;

import java.awt.EventQueue;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import View.MainWindow;

import model.Office;

public class OfficePresenter 
{
	private Office myOfficeModel;
	public static MainWindow mainWindow;
	
	public OfficePresenter()
	{
		myOfficeModel = new Office("");
	}
	/**
	 * This is Dr. Waters' implementation for saving an office to a file.  Since we are doing a database
	 * approach, this can be scrapped later.
	 * 
	 * @param filename
	 */
	public void save(String filename) {
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
			out.writeObject(myOfficeModel);
		} catch (FileNotFoundException e) {
			System.err.println("Failed to open file " + filename + " for output");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Failed during save for unknown reason");
			e.printStackTrace();
		}
	}
	/**
	 * Dr. Waters' saving from file implementation.  Can be scrapped later.
	 * 
	 * @param filename
	 */
	public void load(String filename) {
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));		
			Office model = (Office) in.readObject();
			myOfficeModel.setName(model.getName());
			myOfficeModel.setEmployees(model.getEmployeeListing());
		} catch (FileNotFoundException e) {
			System.err.println("Could not find file " + filename + " for opening");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Other input failure during object read");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.err.println("Failed to find class type on object read");
			e.printStackTrace();
		}
	}
	
	/**
	 * I don't know how we are going to handle logging-in, so, as of right now, all this function does
	 * is to scrap the login window and bring up the main window.
	 * 
	 * @param userID
	 * @param password
	 * @param role
	 */
	public static void login(String userID, String password, int role)
	{
		mainWindow = new MainWindow(role);
		Driver.loginScreen.dispose();
	}
	
	/**
	 * The complexities of this function are beyond the grasp of a mere mortal.
	 */
	public void logOut()
	{
		mainWindow.close();
		Driver.loginScreen.initialize();
	}
}
